﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FB.Bussiness;
using FB.DataAccess;

namespace FB.Videos.ucCommon
{
    public partial class ucPlayVideo : System.Web.UI.UserControl
    {
        /// <summary>
        ///  View State ObjectID
        /// </summary>
        public string ObjectID
        {
            get
            {
                object obj = this.ViewState["_ObjID"];
                if (obj == null)
                    return ""; //default to show ID : 0 
                else
                    return (string)obj;
            }
            set
            {
                this.ViewState["_ObjID"] = value;
            }

        }

        /// <summary>
        ///  View State VideoTitle
        /// </summary>
        public string VideoTitle
        {
            get
            {
                object obj = this.ViewState["_VideoTitle"];
                if (obj == null)
                    return null;
                else
                    return (string)obj;
            }
            set
            {
                this.ViewState["_VideoTitle"] = value;
            }

        }

        /// <summary>
        ///  View State ViewCount
        /// </summary>
        public double ViewCount
        {
            get
            {
                object obj = this.ViewState["_ViewCount"];
                if (obj == null)
                    return 0;
                else
                    return (double)obj;
            }
            set
            {
                this.ViewState["_ViewCount"] = value;
            }

        }

        /// <summary>
        ///  View State Duration
        /// </summary>
        public string Duration
        {
            get
            {
                object obj = this.ViewState["_Duration"];
                if (obj == null)
                    return "0";
                else
                    return (string)obj;
            }
            set
            {
                this.ViewState["_Duration"] = value;
            }

        }

        /// <summary>
        ///  View State ContentURL
        /// </summary>
        public string ContentURL
        {
            get
            {
                object obj = this.ViewState["_ContentURL"];
                if (obj == null)
                    return "";
                else
                    return (string)obj;
            }
            set
            {
                this.ViewState["_ContentURL"] = value;
            }

        }

        /// <summary>
        ///  View State EmbedURL
        /// </summary>
        public string EmbedURL
        {
            get
            {
                object obj = this.ViewState["_EmbedURL"];
                if (obj == null)
                    return "";
                else
                    return (string)obj;
            }
            set
            {
                this.ViewState["_EmbedURL"] = value;
            }

        }
        /// <summary>
        ///  View State thumbnailUrl
        /// </summary>
        public string ThumbnailUrl
        {
            get
            {
                object obj = this.ViewState["_ThumbnailUrl"];
                if (obj == null)
                    return "";
                else
                    return (string)obj;
            }
            set
            {
                this.ViewState["_ThumbnailUrl"] = value;
            }

        }
        
        /// <summary>
        ///  View State thumbnailUrl
        /// </summary>
        public DateTime UploadDate
        {
            get
            {
                object obj = this.ViewState["_UploadDate"];
                if (obj == null)
                    return DateTime.Today;
                else
                    return (DateTime)obj;
            }
            set
            {
                this.ViewState["_UploadDate"] = value;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            VideoBO BO = new VideoBO();
            // Get this video
            Video CurrentVideo = BO.GetVideoByYouTubeID(ObjectID);
            VideoTitle = CurrentVideo.Title;
            ViewCount = (double)CurrentVideo.ViewCount;
            Duration = FB.Utilities.Common.Second2Munite((double)CurrentVideo.Duration);
            ContentURL = "http://danhchothieunhi.com/Videos/Index.aspx?v=" + CurrentVideo.YoutubeID;
            EmbedURL = "http://www.youtube.com/embed/" + CurrentVideo.YoutubeID + "?version=3&autoplay=1&controls=1&hl=vi&modestbranding=1&showinfo=0";
            ThumbnailUrl = "http://i.ytimg.com/vi/" + CurrentVideo.YoutubeID + "/mqdefault.jpg";
            UploadDate = CurrentVideo.CreatedDate;
            //Increase view count
            BO.IncreaseViewCount(ObjectID);
        }
    }
}